package me.magicall.test.coll;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
	private int parentId;
	private int selfId;
	protected String nodeName;
	protected Object obj;
	protected TreeNode parentNode;
	protected List<TreeNode> childList;

	public TreeNode() {
		initChildList();
	}

	public TreeNode(final TreeNode parentNode) {
		setParentNode(parentNode);
		initChildList();
	}

	public boolean isLeaf() {
		return childList == null || childList.isEmpty();
	}

	/* 插入一个child节点到当前节点中 */
	public void addChildNode(final TreeNode treeNode) {
		initChildList();
		childList.add(treeNode);
	}

	protected void initChildList() {
		if (childList == null) {
			childList = new ArrayList<TreeNode>();
		}
	}

	public boolean isValidTree() {
		return true;
	}

	/* 返回当前节点的父辈节点集合 */
	public List<TreeNode> getElders() {
		final List<TreeNode> elderList = new ArrayList<TreeNode>();
		final TreeNode parentNode = getParentNode();
		if (parentNode == null) {
			return elderList;
		} else {
			elderList.add(parentNode);
			elderList.addAll(parentNode.getElders());
			return elderList;
		}
	}

	/* 返回当前节点的晚辈集合 */
	public List<TreeNode> getJuniors() {
		final List<TreeNode> juniorList = new ArrayList<TreeNode>();
		final List<TreeNode> childList = getChildList();
		if (childList == null) {
			return juniorList;
		} else {
			final int childNumber = childList.size();
			for (int i = 0; i < childNumber; i++) {
				final TreeNode junior = childList.get(i);
				juniorList.add(junior);
				juniorList.addAll(junior.getJuniors());
			}
			return juniorList;
		}
	}

	/* 返回当前节点的孩子集合 */
	public List<TreeNode> getChildList() {
		return childList;
	}

	/* 删除节点和它下面的晚辈 */
	public void deleteNode() {
		final TreeNode parentNode = getParentNode();
		final int id = getSelfId();

		if (parentNode != null) {
			parentNode.deleteChildNode(id);
		}
	}

	/* 删除当前节点的某个子节点 */
	public void deleteChildNode(final int childId) {
		final List<TreeNode> childList = getChildList();
		final int childNumber = childList.size();
		for (int i = 0; i < childNumber; i++) {
			final TreeNode child = childList.get(i);
			if (child.getSelfId() == childId) {
				childList.remove(i);
				return;
			}
		}
	}

	/* 动态的插入一个新的节点到当前树中 */
	public boolean insertJuniorNode(final TreeNode treeNode) {
		final int juniorParentId = treeNode.getParentId();
		if (parentId == juniorParentId) {
			addChildNode(treeNode);
			return true;
		} else {
			final List<TreeNode> childList = getChildList();
			final int childNumber = childList.size();
			boolean insertFlag;

			for (int i = 0; i < childNumber; i++) {
				final TreeNode childNode = childList.get(i);
				insertFlag = childNode.insertJuniorNode(treeNode);
				if (insertFlag == true) {
					return true;
				}
			}
			return false;
		}
	}

	/* 找到一颗树中某个节点 */
	public TreeNode findTreeNodeById(final int id) {
		if (selfId == id) {
			return this;
		}
		if (childList.isEmpty() || childList == null) {
			return null;
		} else {
			final int childNumber = childList.size();
			for (int i = 0; i < childNumber; i++) {
				final TreeNode child = childList.get(i);
				final TreeNode resultNode = child.findTreeNodeById(id);
				if (resultNode != null) {
					return resultNode;
				}
			}
			return null;
		}
	}

	/* 遍历一棵树，层次遍历 */
	public void traverse() {
		if (selfId < 0) {
			return;
		}
		print(selfId);
		if (childList == null || childList.isEmpty()) {
			return;
		}
		final int childNumber = childList.size();
		for (int i = 0; i < childNumber; i++) {
			final TreeNode child = childList.get(i);
			child.traverse();
		}
	}

	public void print(final String content) {
		System.out.println(content);
	}

	public void print(final int content) {
		System.out.println(String.valueOf(content));
	}

	public void setChildList(final List<TreeNode> childList) {
		this.childList = childList;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(final int parentId) {
		this.parentId = parentId;
	}

	public int getSelfId() {
		return selfId;
	}

	public void setSelfId(final int selfId) {
		this.selfId = selfId;
	}

	public TreeNode getParentNode() {
		return parentNode;
	}

	public void setParentNode(final TreeNode parentNode) {
		this.parentNode = parentNode;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(final String nodeName) {
		this.nodeName = nodeName;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(final Object obj) {
		this.obj = obj;
	}
}
